package cn.wit.summit.chwan.widgets;

import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.facebook.drawee.view.SimpleDraweeView;
import com.google.gson.Gson;

import java.util.List;

import cn.wit.summit.chwan.R;
import cn.wit.summit.chwan.models.db.DownLoadTaskTable;
import cn.wit.summit.chwan.models.dbmanager.DownloadTaskManager;
import cn.wit.summit.chwan.models.dto.GameInfoDetialV2Bean;
import cn.wit.summit.chwan.models.dto.GameListBaseBean;
import cn.wit.summit.chwan.models.dto.GameTagInfoBean;
import cn.wit.summit.chwan.models.response.IndexBeanV3Response;
import cn.wit.summit.chwan.tools.downloadtool.DownloadFactory;
import cn.wit.summit.chwan.utils.IntentUtil;
import cn.wit.summit.chwan.utils.StringUtils;
import cn.wit.summit.chwan.utils.UtilsMy;

/**
 * Created by Alan on 2018/3/13.
 */

public class ItemListViewV2 extends LinearLayout implements View.OnClickListener {

    private Gson    mGson    = null;
    private Context mContext = null;
    private DownloadFactory factory;
    private LayoutParams    params;
    private Uri             uri;
    private DownLoadTaskTable mDownLoadTaskTable = null;
    private ItemDownloadInterface mItemDownloadInterface;

    private RelativeLayout     itemDownloadRootRl;
    private SimpleDraweeView   itemDownloadImg; //任务图片
    private TextView           itemDownloadNameTx;      //任务名字
    private TextView           itemDownloadMoneyTx;      //任务奖励
    private TextView           itemDownloadTagTx;      //
    private TextView           itemDownloadDecTx;      //
    private TextView           itemDownloadTypeTx;  //
    private DownloadViewStrokeV2 itemDownloadBtn;       //

    public ItemListViewV2(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        initView(context, attrs);
    }

    public ItemListViewV2(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView(context, attrs);
    }

    private void initView(Context context, AttributeSet attrs) {
        this.mContext = context;
        mGson = new Gson();
        uri = Uri.parse("res:///" + R.drawable.main_normal_icon);
        params = new LayoutParams(
                LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        params.setMargins(5, 0, 5, 0);
        params.gravity = Gravity.CENTER;
        factory = DownloadFactory.get();

        View rootView = LayoutInflater.from(context).inflate(R.layout.item_download_v2_view, null);
        itemDownloadRootRl = rootView.findViewById(R.id.itemDownloadRootRl);
        itemDownloadImg = rootView.findViewById(R.id.itemDownloadImg);
        itemDownloadNameTx = rootView.findViewById(R.id.itemDownloadNameTx);
        itemDownloadTagTx = rootView.findViewById(R.id.itemDownloadTagTx);
        itemDownloadDecTx = rootView.findViewById(R.id.itemDownloadDecTx);
        itemDownloadTypeTx = rootView.findViewById(R.id.itemDownloadTypeTx);
        itemDownloadMoneyTx = rootView.findViewById(R.id.itemDownloadMoneyTx);
        itemDownloadBtn = rootView.findViewById(R.id.itemDownloadBtn);
        itemDownloadBtn.setOnClickListener(this);

        itemDownloadRootRl.setOnClickListener(this);
        itemDownloadRootRl.setOnLongClickListener(new OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                if (mItemDownloadInterface != null) {
                    mItemDownloadInterface.onAdapterItemLongChildClick(v, mDownLoadTaskTable);
                    return true;
                } else {
                    return false;
                }
            }
        });

        this.removeAllViews();
        this.addView(rootView);
    }

    GameListBaseBean gameListBaseBean;
    Activity         activity;

    public void setData(Activity activity, GameInfoDetialV2Bean gameListBaseBean) {
        this.activity = activity;
        setData(gameListBaseBean, true);
    }

    public void setDataNotagInfo(Activity activity, GameInfoDetialV2Bean gameListBaseBean) {
        this.activity = activity;
        setData(gameListBaseBean, false);

    }

    private void setData(GameInfoDetialV2Bean gameListBaseBean, boolean showTagInfo) {
        this.gameListBaseBean = gameListBaseBean;
        itemDownloadImg.setImageURI(gameListBaseBean.getGame_ico());
        itemDownloadNameTx.setText(gameListBaseBean.getGame_name());

        String tag = gameListBaseBean.getGame_title_v1().replace("|", "· ");
        itemDownloadTagTx.setText(tag);
        itemDownloadDecTx.setText(gameListBaseBean.getGame_system_ver());
        itemDownloadTypeTx.setText(gameListBaseBean.getGame_title_v2());
        itemDownloadBtn.initData(gameListBaseBean);

        IndexBeanV3Response.CreditInfoBean temp = gameListBaseBean.getCredit_info();
        DownLoadTaskTable taskTable = gameListBaseBean.getTaskTable();
        if (taskTable == null) {
            taskTable = gameListBaseBean.getDownloadTaskTableSimple();

            taskTable = DownloadTaskManager.getInstance().findByGameId(gameListBaseBean.getGame_id());
        }
        if (temp!=null && temp.getDay_surplus()>0
                && temp.getWeek_surplus()>0
                && temp.getGame_down_load_reward()>0
                && (taskTable==null || taskTable.getDownStatus()==0)) {
            itemDownloadMoneyTx.setVisibility(VISIBLE);
            itemDownloadMoneyTx.setText("+ "+temp.getGame_down_load_reward()+"元");
        } else {
            itemDownloadMoneyTx.setVisibility(INVISIBLE);
        }
    }

    public String getDataGameId() {
        return this.gameListBaseBean.getGame_id();
    }

    public DownloadViewStrokeV2 getDownView() {
        return itemDownloadBtn;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {

            case R.id.itemDownloadRootRl://item点击
                IntentUtil.getInstance().goGamedetial(activity, gameListBaseBean.getGame_id());
                if (mOnClickListener != null) mOnClickListener.onClick(v);
                break;
        }
    }

    OnClickListener mOnClickListener;

    @Override
    public void setOnClickListener(@Nullable OnClickListener l) {
        super.setOnClickListener(l);
        mOnClickListener = l;
    }

    public void setOnclickLinstener(ItemListViewV2.ItemDownloadInterface itemDownloadInterface) {
        mItemDownloadInterface = itemDownloadInterface;
    }

    public interface ItemDownloadInterface {
        void onAdapterItemLongChildClick(View view, DownLoadTaskTable taskTable);
    }
}
